﻿using System;
using System.Windows.Forms;
using System.Xml;

using ClassLibrary;
using Core;

namespace TestProject
{
    public partial class NewPop : Form
    {
        private Species specie;
        private Model model;
        public NewPop(Model m,Species sp)
        {
            InitializeComponent();
            specie = sp;
            model = m;
        }

        private void create_butt_Click(object sender, EventArgs e)
        {
            if (System.IO.File.Exists("file_input\\" + model.model_name + "\\Species\\" + specie.name + "\\Populations\\" + name_box.Text + "\\population.xml"))
            {
                MessageBox.Show("A population with this name already exists. Please, choose another name.");
                return;
            }
            System.IO.Directory.CreateDirectory("file_input\\" + model.model_name + "\\Species\\" + specie.name + "\\Populations\\" + name_box.Text);
            XmlTextWriter writer = new XmlTextWriter("file_input\\" + model.model_name + "\\Species\\" + specie.name + "\\Populations\\" + name_box.Text + "\\population.xml",null);
            writer.Formatting = Formatting.Indented;
            writer.WriteStartDocument();
            writer.WriteStartElement("population");
            writer.WriteAttributeString("Name", name_box.Text);
            writer.WriteAttributeString("AmountIndividuals", "0");
            writer.WriteStartElement("individuals");
            writer.WriteEndElement();
            writer.WriteEndElement();
            writer.WriteEndDocument();
            writer.Close();
            MyXmlDocument doc = new MyXmlDocument();
            doc.LoadXml(System.IO.File.ReadAllText(specie.path));
            XmlElement el;
            el = doc.CreateElement("population");
            el.SetAttribute("PathToPopulation", "file_input\\" + model.model_name + "\\Species\\" + specie.name + "\\Populations\\" + name_box.Text + "\\population.xml");
            doc.Increment(doc.DocumentElement.Attributes[1]);
            doc.GetElementsByTagName("populations")[0].AppendChild(el);
            doc.Save(specie.path);
            Functions.add_node_func(new Population("file_input\\" + model.model_name + "\\Species\\" + specie.name + "\\Populations\\" + name_box.Text + "\\population.xml",specie), name_box.Text, "pop");
            this.Close();
        }

        private void canc_butt_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
